<template>
    <el-form
      label-position="right"
      label-width="100px"
      :model="user"
      style="max-width: 460px"
    >
      <el-form-item label="账号">
        <el-input v-model="user.adminname" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="user.password" />
      </el-form-item>
      <el-form-item >
        <el-button @click="loginCheck">登录</el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script lang="ts" setup>
  import { reactive, ref } from 'vue'
  import {useRouter} from "vue-router";
  import { ElMessage } from 'element-plus';
  import {loginApi} from "@/api/user";
  import type {IOper} from "@/myTypes";
import {useUserStore} from "@/stores/grantsStore";

const {doLogin} = useUserStore();


  const user = reactive<IOper>({
    adminname:"上课用勿删",
    password:"123"
  })

  const $router = useRouter()

  async function loginCheck(){
    const res = await loginApi(user);

    if(res.data.code==="200"){
        ElMessage({
            type:"success",
            message:"登录成功！"
        })
        doLogin();//
        // 保存用户信息：
        sessionStorage.setItem("adminname",res.data.data.adminname);
        // 保存token
        sessionStorage.setItem("token",res.data.data.token);
        // 保存权限信息：
        sessionStorage.setItem("checkedKeys",JSON.stringify(res.data.data.checkedkeys));

        $router.push("/Admin");

    }else {
        ElMessage({
            type:"error",
            message:"登录失败，账号或密码错误"
        })
    }
  }

  </script>@/grants/routes